package com.ruoyi.wisdom.event;

import com.ruoyi.wisdom.domain.IotStudentLeave;
import com.ruoyi.wisdom.domain.swap.SwapStudentLeave;
import com.ruoyi.wisdom.service.IIotStudentLeaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class StudentLeaveEvent extends IotEvent {

    @Autowired
    private IIotStudentLeaveService iotStudentLeaveService;

    /**
     * 处理
     *
     * @param swapStudentLeave
     */
    public void handle(SwapStudentLeave swapStudentLeave) {
        if (swapStudentLeave.getOperate() == null) {
            save(swapStudentLeave);
        } else {
            switch (swapStudentLeave.getOperate()) {
                case "delete":
                case "remove":
                    delete(swapStudentLeave);
                    break;
                default:
                    save(swapStudentLeave);
            }
        }
    }

    /**
     * 添加|编辑
     *
     * @param swapStudentLeave
     */
    public void save(SwapStudentLeave swapStudentLeave) {
        IotStudentLeave result = iotStudentLeaveService.selectIotStudentLeaveById(swapStudentLeave.getId());
        // 实体装换
        IotStudentLeave iotStudentLeave = getModelMapper().map(swapStudentLeave, IotStudentLeave.class);
        if (result != null) {
            iotStudentLeave.setId(result.getId());
            iotStudentLeaveService.updateIotStudentLeave(iotStudentLeave);
        } else {
            iotStudentLeaveService.insertIotStudentLeave(iotStudentLeave);
        }
    }

    /**
     * 删除
     *
     * @param swapStudentLeave
     */
    public void delete(SwapStudentLeave swapStudentLeave) {
        iotStudentLeaveService.deleteIotStudentLeaveById(swapStudentLeave.getId());
    }
}
